import { createRouter, createWebHistory } from 'vue-router'


import UserListView from "@/views/user/ListView";
import UserSaveView from "@/views/user/SaveView";


import FoodListView from "@/views/food/ListView";
import FoodSaveView from "@/views/food/SaveView";

import CategoryListView from "@/views/category/ListView";
import FlavorListView from "@/views/flavor/ListView";

import BookListView from "@/views/book/ListView";
import BookDetailView from "@/views/book/DetailView";

import FeedbackListView from "@/views/feedback/ListView";
import StoreListView from "@/views/store/StoreList";

import StoreSaveView from "@/views/store/SaveView"

const routes = [

  {
    path: '/',
    component: () => import( '../views/layout/LayoutView.vue'),
    children:[
      {
        path:"/",
        redirect:"/user/list"
      },
      {
        path: '/user/list',
        name: 'userListView',
        component:UserListView
      },
      {
        path: '/user/save',
        name: 'userSaveView',
        component:UserSaveView
      },{
        path: '/category/list',
        name: 'categoryListView',
        component:CategoryListView
      },{
        path: '/flavor/list',
        name: 'flavorListView',
        component:FlavorListView
      },{
        path: '/food/list',
        name: 'foodListView',
        component:FoodListView
      },{
        path: '/food/save',
        name: 'foodSaveView',
        component:FoodSaveView
      },
      {
        path: '/store/save',
        name: 'StoreSaveView',
        component:StoreSaveView
      },
      {
        path: '/book/list',
        name: 'bookListView',
        component:BookListView
      },{
        path: '/book/:num',
        name: 'bookDetailView',
        component:BookDetailView
      },{
        path: '/feedback/list',
        name: 'feedbackList',
        component:FeedbackListView
      },{
        path: '/store/list',
        name: 'storeList',
        component:StoreListView
      }
      
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/LoginView.vue')
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


router.beforeEach((to, from, next) => {

  let token = sessionStorage.getItem("token");
  let toPath = to.path;
  if(toPath=="/login"){
    //判断是否已经登录过了
    if(token){
      next({"path":"/user/list"});
    }else{
      next();
    }
  }else{
    //判断是否已经登录
    if(token){
      //已经登录了
      next();
    }else{
      next({"path":"/login"});
    }
  }
})

export default router
